<?php 



/**
 * 
 * 文件上传函数
 * @param 上传的文件 $pic
 * @param 上传的路径 /Public/upload $url
 * @return 成功返回路径  失败返回失败原因
 */
function upload($pic, $url='img') {
    $upload = new \Think\Upload(); // 实例化上传类
    $upload->maxSize = 10485760; // 设置附件上传大小
    $upload->exts = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
    $upload->rootPath = 'Public/';
    $upload->savePath = 'upload/' . $url . '/'; // 设置附件上传目录
    $info = $upload->uploadOne($pic);

    if (!$info) {// 上传错误提示错误信息
        $res = array('status' => '-1', 'msg' => $upload->getError());
    } else {// 上传成功 删除原来的图片
        $res = '/' . $upload->rootPath . '/' . $info['savepath'] . $info['savename'];
    }
    return $res;
}




function get_real_ip(){
    $ip=false;
    if(!empty($_SERVER['HTTP_CLIENT_IP'])){
        $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    
    if(!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
        $ips=explode (', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
        if($ip){ array_unshift($ips, $ip); $ip=FALSE; }

        for ($i=0; $i < count($ips); $i++){
            if(!eregi ('^(10│172.16│192.168).', $ips[$i])){
                $ip=$ips[$i];
                break;
            }
        }
    }

    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);

}


/**
 * 菜单 节点重组
 * @param  [type]  $node   [数组]
 * @param  [type]  $access [对比有没有权限]
 * @param  integer $pid    [父ID ]
 * @return [type]          [返回结果]
 */
function node_merge($node , $access , $pid=0){
    $arr = [];
    foreach ($node as $k => $v) {
        if (is_array($access)) {
            $v['access'] = in_array($v['id'] , $access) ? 1 : 0;
        }
        
        if ($v['pid'] == $pid) {
            $v['child'] = node_merge($node , $access , $v['id']);
            $arr[] = $v;
        }
    }
    return $arr;
}



/**
 * [多维数组转二维 description]
 * @param  [type] $arr [description]
 * @return [type]      [description]
 */
function arrayTurnTwo($arr , $access){
    $data = [];
    foreach ($arr as $k=>$v) {
        // 多维数组 合并到一个新数组
        $data = array_merge($data , $v['node']);

    }

    /*二维数组去掉重复值 并保留键值*/
    $data = array_unique_fb($data);

    // return count($data);
    // 菜单 节点重组 ， $access 用户拥有权项
    return node_merge($data, $access , 1);
}




//二维数组去掉重复值
function arrayUnique($array2D){
    foreach ($array2D as $v){
        $v=join(',',$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
        $temp[]=$v;
    }

    $temp=array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
    foreach ($temp as $k => $v){
        $temp[$k]=explode(',',$v); //再将拆开的数组重新组装
    }
    return $temp;
}




//二维数组去掉重复值 并保留键值
function array_unique_fb($array2D){
    foreach ($array2D as $k=>$v){

        $v = join(",",$v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
        $temp[$k] = $v;
    }
    
    $temp = array_unique($temp); //去掉重复的字符串,也就是重复的一维数组
    foreach ($temp as $k => $v){

        $array=explode(",",$v); //再将拆开的数组重新组装
        $temp2[$k]["id"] =$array[0];
        $temp2[$k]["name"] =$array[1];
        $temp2[$k]["title"] =$array[2];
        $temp2[$k]["en_title"] =$array[3];
        $temp2[$k]["status"] =$array[4];
        $temp2[$k]["remark"] =$array[5];
        $temp2[$k]["sort"] =$array[6];
        $temp2[$k]["pid"] =$array[7];
        $temp2[$k]["level"] =$array[8];
    }
    return $temp2;
}





/**
 * 【模板中组合URL】
 * @param  [type] $contr  [控制器]
 * @param  [type] $action [方法]
 * @return [type]         [description]
 */
function _url($contr , $action){

    return U($contr . '/' . $action);
}



/**
 * 获取用户的角色ID
 * @param  [type] $arr [description]
 * @return [type]      [description]
 */
function getRoleOrId($arr){
    $data = [];
    foreach ($arr['role'] as $k => $v) {
        $data[] = $v['id'];
    }
    return $data;
}




/**
 * 对传过来的值做处理，无用的字段删除
 * 正则 匹配
 * @param  [type] $data [description]
 * @return [type]       [description]
 */
function unsetField($data){

    $preg = ['/^think/','/^pgv/','/^Hm_/','/^PHPSESSID/','/^VID_100678/'];
    $newArr = array_values(array_flip($data));
    foreach ($newArr as $k => $v) {
        foreach ($preg as $kk => $vv) {
            if (preg_match($vv , $newArr[$k])) {
                unset($newArr[$k]);
            }
        }
    }

    $c = [];
    $newArrTwo = array_flip($newArr);
    foreach ($newArrTwo as $k => $v) {
        foreach ($data as $kk => $vv) {
            if ($k == $kk) {
                $c[$kk] = $vv;
            }
        }
    }
    return $c;
}



/**
 * 自动写入日志功能
 * 做到记录用户操作轨迹
 * @param  [type] $data [description]
 * @return [type]       [description]
 */
function autoAddLog($data){
    $newArr = unsetField($data);

    $arr['uid']        = session(C('USER_AUTH_KEY'));
    $arr['action']     = CONTROLLER_NAME . '/' . ACTION_NAME;
    $arr['note']       = json_encode($newArr);
    $arr['ip']         = get_client_ip();
    $arr['add_time']   = time();

    // if (!empty($newArr)) {
        M('log')->add($arr);
    // }
}


/**
 * 返回 layui 表格数据形式
 * @param  [type]  $data [description]
 * @param  integer $code [description]
 * @param  string  $msg  [description]
 * @return [type]        [description]
 */
function returnYlauiJsonData($data , $pageCount , $code = 0 , $msg = ''){

    $res['code'] = $code;
    $res['msg'] = $msg;
    $res['count'] = $pageCount;
    $res['data'] = $data;
    return json_encode($res);
}


/**
 * 获取 GET 或 POST 数据
 * @param  [type] $arr [description]
 * @return [type]      [description]
 */
function getOrPostVal($arr){
    $where = [];
    foreach ($arr as $k => $v) {
        if (!empty($v)) {
            $where[$k] = trim($v);
        }
    }
    return $where;
}






?>